from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QComboBox, QTabWidget
from PyQt5.QtGui import QFont

class ClipboardMonitorTab(QWidget):
    def __init__(self, system):
        super().__init__()
        self.system = system
        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()

        font = QFont()
        font.setPointSize(12)  # 设置字体大小

        self.description_label = QLabel('说明: 该工具会监控剪切板内容的变化，并自动使用选定的搜索引擎进行查询。')
        self.description_label.setFont(font)
        layout.addWidget(self.description_label)

        self.url_label = QLabel('Search URL:')
        self.url_label.setFont(font)
        layout.addWidget(self.url_label)

        self.url_entry = QLineEdit(self)
        self.url_entry.setFont(font)
        self.url_entry.setText(self.system.search_engines[self.system.default_engine])
        layout.addWidget(self.url_entry)

        self.engine_label = QLabel('Select Search Engine:')
        self.engine_label.setFont(font)
        layout.addWidget(self.engine_label)

        self.engine_combo = QComboBox(self)
        self.engine_combo.setFont(font)
        self.engine_combo.addItems(self.system.search_engines.keys())
        self.engine_combo.setCurrentText(self.system.default_engine)
        self.engine_combo.currentTextChanged.connect(self.change_engine)
        layout.addWidget(self.engine_combo)

        self.start_button = QPushButton('Start', self)
        self.start_button.setFont(font)
        self.start_button.clicked.connect(self.start_monitoring)
        layout.addWidget(self.start_button)

        self.stop_button = QPushButton('Stop', self)
        self.stop_button.setFont(font)
        self.stop_button.clicked.connect(self.stop_monitoring)
        layout.addWidget(self.stop_button)

        self.setLayout(layout)

    def change_engine(self, text):
        self.system.default_engine = text
        self.url_entry.setText(self.system.search_engines[text])
        if self.system.running:
            clipboard_text = self.system.get_clipboard_text()
            self.system.search_information(clipboard_text)

    def start_monitoring(self):
        self.system.start_monitoring()
        self.start_button.setEnabled(False)
        self.stop_button.setEnabled(True)
        # 强制触发一次查询
        clipboard_text = self.system.get_clipboard_text()
        self.system.search_information(clipboard_text)

    def stop_monitoring(self):
        self.system.stop_monitoring()
        self.start_button.setEnabled(True)
        self.stop_button.setEnabled(False)